import type { RequestData } from '@ant-design/pro-table';
import { request } from 'umi';
import { SpaceItem, SpaceQueryBean, SpaceRecordItem, SpaceRecordQueryBean } from './types';
import { SUCCESS_CODE } from '@/utils/request';
import { message } from 'antd';

//分页查询泊位
export async function pageQuerySpace(params: SpaceQueryBean): Promise<RequestData<SpaceItem>> {
  const res = await request('/space/view', {
    params: {
      page: 1,
      pageSize: 12,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

//分页查询泊位历史
export async function pageQuerySpaceHistory(params: SpaceRecordQueryBean): Promise<RequestData<SpaceRecordItem>> {
  const res = await request(`/space_record/${params.space_id}/view_history`, {
    params: {
      page: 1,
      pageSize: 12,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

